package com.xoa.controller.nccloud;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xoa.model.nccloud.*;

import com.xoa.service.nccloud.*;

import com.xoa.util.common.wrapper.BaseWrapper;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * NCC 视图数据同步类
 */
@Controller
@RequestMapping(value="/nccloud/api/view")
public class SyncViewController {

    // NCC 接口操作类
    @Autowired
    private INCCloudService nccloudService;

    //NCC 接口日志操作类
    @Autowired
    INCCInterFaceLogService logService;

    //NCC 视图操作类
    @Autowired
    INCCViewService viewService;

    @Autowired
    NccPostVService nccPostVService;

    @Autowired
    NccPsnclVService nccPsnclVService;
    @Autowired
    BdPsndocZzVService bdPsndocZzVService;

    @Autowired
    NccPostseriesVService nccPostseriesVService;

    /*
     * 待转正人员
     * */
    @RequestMapping("bd_psndoc_zz_v")
    @ResponseBody
    public BaseWrapper querypsndoc() throws Exception {
        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            String requstBody = "{\"view\":\"bd_psndoc_zz_v \"}";
            int  add=0,modify=0,del=0;
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONObject resultJson = JSONObject.parseObject(result);

            if (resultJson.getIntValue("code") == 1) {
                if (null != resultJson.getJSONArray("data").toJSONString() && !resultJson.getJSONArray("data").toJSONString().equals("[]")) {
                    //清空表
                    viewService.truncateTable("bd_psndoc_zz_v");
                    /*List<BdPsndocZzV> psninfoList = bdPsndocZzVService.getBdPsndocZzVListByMap(null);
                    Map<String, Object> psnMap = new HashMap<>();
                    if (null != psninfoList && psninfoList.size() != 0) {
                        for (BdPsndocZzV psninfo : psninfoList) {
                            psnMap.put(psninfo.getCode(), psninfo);
                        }
                    }*/
                    JSONArray dataArray = resultJson.getJSONArray("data");
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject jsonObject = resultJson.getJSONArray("data").getJSONObject(i);
                        BdPsndocZzV bdPsndocZzV = new BdPsndocZzV();
                        bdPsndocZzV.setPostname(jsonObject.getString("postname"));
                        bdPsndocZzV.setBdPsndocZzVId(jsonObject.getString("id"));
                        bdPsndocZzV.setClerkcode(jsonObject.getString("clerkcode"));
                        bdPsndocZzV.setCode(jsonObject.getString("code"));
                        bdPsndocZzV.setDeptName(jsonObject.getString("dept_name"));
                        bdPsndocZzV.setIdtype(jsonObject.getString("idtype"));
                        bdPsndocZzV.setIsmainjob(jsonObject.getString("ismainjob"));
                        bdPsndocZzV.setName(jsonObject.getString("name"));
                        bdPsndocZzV.setOrgName(jsonObject.getString("org_name"));
                        bdPsndocZzV.setPkDept(jsonObject.getString("pk_dept"));
                        bdPsndocZzV.setPkOrg(jsonObject.getString("pk_org"));
                        bdPsndocZzV.setPkPsncl(jsonObject.getString("pk_psncl"));
                        bdPsndocZzV.setPkPsndoc(jsonObject.getString("pk_psndoc"));
                        bdPsndocZzV.setPkPsnjob(jsonObject.getString("pk_psnjob"));
                        bdPsndocZzV.setPkPsnorg(jsonObject.getString("pk_psnorg"));
                        bdPsndocZzV.setPkPost(jsonObject.getString("pk_post"));
                        add++;
                        bdPsndocZzVService.itriptxAddBdPsndocZzV(bdPsndocZzV);
                        /*if (!psnMap.containsKey(bdPsndocZzV.getCode())) {
                            // 不存在的 add
                            add++;
                            bdPsndocZzVService.itriptxAddBdPsndocZzV(bdPsndocZzV);
                        } else {
                            // 存在的 修改
                            modify++;
                            bdPsndocZzVService.itriptxModifyBdPsndocZzV(bdPsndocZzV);
                            //在同步表删除已修改过的
                            psnMap.remove(bdPsndocZzV.getCode());
                        }*/
                    }
                    //剩下未修改的进行删除
                    /*if (psnMap.size() != 0 && null != psnMap) {

                        Set<Map.Entry<String, Object>> entries = psnMap.entrySet();
                        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                        while (iterator.hasNext()) {
                            bdPsndocZzVService.itriptxDeleteBdPsndocZzVById(((BdPsndocZzV) iterator.next().getValue()).getId());
                            del++;
                        }
                    }*/
                } else {
                    throw new RuntimeException("请求NC视图data-null");
                }
            }else {
                throw  new RuntimeException(resultJson.getString("message"));
            }


            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("bd_psndoc_zz_v");
            ncclog.setContent(requstBody);
            ncclog.setName("待转正视图人员添加成功添加"+add+"修改"+modify+"删除"+del);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);




            baseWrapper.setFlag(true);
            baseWrapper.setMsg("待转正视图成功新增人员：" + add + ";修改人员：" + modify+"删除"+del);
            baseWrapper.setStatus(true);


        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("待转正视图失败:|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("bd_psndoc_zz_v");
            ncclog.setContent("");
            ncclog.setName("待转正视图失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }



    /*
     * 行政级别 hr_bd_defdoc_xzjb_v、任职类型 hr_bd_defdoc_rzlx_v、任职方式 hr_bd_defdoc_rzfs_v、异动原因 bd_defdoc_ydyy_v
     * ji'z'z'y
     * */
    @RequestMapping("defdoc")
    @ResponseBody
    public BaseWrapper defdoc(String view) throws Exception {

        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            if (StringUtils.isBlank(view)){
                baseWrapper.setFlag(false);
                baseWrapper.setMsg("视图名称不能为空");
                return baseWrapper;
            }

            String requstBody = "{\"view\":\""+ view +"\"}";
            int  add=0;
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.getIntValue("code") == 1){
                if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                if ("bd_defdoc_ydyy_v".equals(view)){
                    view = "hr_" + view;
                }
                    String tableName=view.replace("hr","ncc");
                viewService.truncateTable(tableName);
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);

                    BaseBdDefdoc defdoc = new BaseBdDefdoc();
                    defdoc.setCode(jsonObject.getString("code"));
                    defdoc.setName(jsonObject.getString("name"));
                    defdoc.setPkDefdoc(jsonObject.getString("pk_defdoc"));
                    defdoc.setTableNme(tableName);
                    viewService.saveDefdoc(defdoc);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
            throw  new RuntimeException(resultJson.getString("message"));
        }

            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo(view);
            ncclog.setContent(requstBody);
            ncclog.setName("视图"+view+"成功清空并添加：" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("视图"+view+"成功清空并添加：" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("视图"+view+"失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo(view);
            ncclog.setContent("");
            ncclog.setName("视图"+view+"失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }


    /*
     *
     *  职务
     *
     * */
    @RequestMapping("hr_om_job_v")
    @ResponseBody
    public BaseWrapper hr_om_job_v(String view) throws Exception {


        BaseWrapper baseWrapper=new BaseWrapper();

        try {
            String requstBody = "{\"view\":\"hr_om_job_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            int  add=0;
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.getIntValue("code") == 1){
                if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                viewService.truncateTable("ncc_om_job_v");
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);

                    NccOmJobV job = new NccOmJobV();
                    job.setPkJob(jsonObject.getString("pk_job"));
                    job.setJobcode(jsonObject.getString("jobcode"));
                    job.setJobname(jsonObject.getString("jobname"));
                    job.setPkOrg(jsonObject.getString("pk_org"));
                    job.setPkJobtype(jsonObject.getString("pk_jobtype"));
                    viewService.saveOmJob(job);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
            throw  new RuntimeException(resultJson.getString("message"));
        }
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_om_job_v");
            ncclog.setContent(requstBody);
            ncclog.setName("职务视图成功清空并添加：" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("职务视图成功清空并添加：" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("职务视图添加失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_om_job_v");
            ncclog.setContent("");
            ncclog.setName("职务视图添加失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }

    /*
     * 职务类别
     * */
    @RequestMapping("hr_om_jobtype_v")
    @ResponseBody
    public BaseWrapper hr_om_jobtype_v(String view) throws Exception {

        BaseWrapper baseWrapper=new BaseWrapper();

        try{
            String requstBody = "{\"view\":\"hr_om_jobtype_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);

            int  add=0;
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.getIntValue("code") == 1){
                if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                viewService.truncateTable("ncc_om_jobtype_v");
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);

                    NccOmJobtypeV job = new NccOmJobtypeV();
                    job.setFatherPk(jsonObject.getString("father_pk"));
                    job.setJobtypecode(jsonObject.getString("jobtypecode"));
                    job.setJobtypename(jsonObject.getString("jobtypename"));
                    job.setPkOrg(jsonObject.getString("pk_org"));
                    job.setPkJobtype(jsonObject.getString("pk_jobtype"));
                    viewService.saveOmJobType(job);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(resultJson.getString("message"));
        }
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_om_jobtype_v");
            ncclog.setContent(requstBody);
            ncclog.setName("职务类别视图成功清空并添加：" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("职务类别视图成功清空并添加：" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("职务类别视图添加失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_om_jobtype_v");
            ncclog.setContent("");
            ncclog.setName("职务类别视图添加失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }

    /*
     * 薪资级别
     *
     * */
    @RequestMapping(value = {"/hr_wa_prmlv_v"})
    @ResponseBody
    public BaseWrapper waprmlv(String view) throws Exception {
        BaseWrapper baseWrapper = new BaseWrapper();


        try {
            //薪级
            String requstBody = "{\"view\":\"hr_wa_prmlv_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONObject resultJson = JSONObject.parseObject(result);

            //薪资标准类别
            String requstBody2 = "{\"view\":\"hr_wa_grade_v\"}";
            String result2 = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody2);
            JSONObject resultJson2 = JSONObject.parseObject(result2);

            int  add=0;



            if (resultJson.getIntValue("code") == 1){
                if(resultJson2.getIntValue("code") == 1){
                    if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                        if(null!=resultJson2.getJSONArray("data").toJSONString()&&!resultJson2.getJSONArray("data").toJSONString().equals("[]")){
            viewService.truncateTable("ncc_wa_prmlv_v");
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                JSONArray dataArray2 = resultJson2.getJSONArray("data");

                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);

                    for (int a=0; a<dataArray2.size(); a++) {
                        if(dataArray2.getJSONObject(a).getString("pk_wa_grd").equals(jsonObject.getString("pk_wa_grd"))){
                            NccWaPrmlvV obj = new NccWaPrmlvV();
                            obj.setPkWaSeclv(jsonObject.getString("pk_wa_seclv"));
                            obj.setPkWaCrt(jsonObject.getString("pk_wa_crt"));
                            obj.setPkWaGrd(jsonObject.getString("pk_wa_grd"));
                            obj.setGrdname(jsonObject.getString("grdname"));
                            obj.setPrmlvname(jsonObject.getString("prmlvname"));
                            obj.setPkWaGradever(jsonObject.getString("pk_wa_gradever"));
                            obj.setSeclvname(jsonObject.getString("seclvname"));
                            obj.setPkWaPrmlv(jsonObject.getString("pk_wa_prmlv"));
                            obj.setCriterionvalue(jsonObject.getString("criterionvalue"));
                            obj.setPkOrg(dataArray2.getJSONObject(a).getString("pk_org"));


                            viewService.saveWaprmlv(obj);
                            add++;
                            break;
                        }
                    }
                }

            }else {
                throw new RuntimeException("请求NChr_wa_grade_v视图data-null");
            }
        }else {
            throw new RuntimeException("请求NChr_wa_prmlv_v视图data-null");
        }
    }else {
        throw new RuntimeException("hr_wa_grade_v:"+resultJson.getString("message"));
    }
}else {
        throw new RuntimeException("hr_wa_prmlv_v:"+resultJson.getString("message"));
        }
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("ncc_wa_prmlv_v");
            ncclog.setContent(requstBody);
            ncclog.setName("薪资级别视图成功清空并添加" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            this.logService.save(ncclog);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("薪资级别视图成功清空并添加" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        } catch (Exception var17) {
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("薪资级别视图失败添加"+var17.getMessage());
            baseWrapper.setStatus(true);
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("ncc_wa_prmlv_v");
            ncclog.setContent("");
            ncclog.setName("薪资级别视图失败添加");
            ncclog.setResultLong(var17.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            this.logService.save(ncclog);
        }

        return baseWrapper;
    }

    /*
     * 薪资标准类别
     * */
    @RequestMapping("hr_wa_grade_v")
    @ResponseBody
    public BaseWrapper wagrade(String view) throws Exception {

        BaseWrapper baseWrapper=new BaseWrapper();

        try {
            String requstBody = "{\"view\":\"hr_wa_grade_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            int  add=0;
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.getIntValue("code") == 1){
                if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                viewService.truncateTable("ncc_wa_grade_v");
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);
                    NccWaGradeV obj = new NccWaGradeV();
                    obj.setName(jsonObject.getString("name"));
                    obj.setPkWaGrd(jsonObject.getString("pk_wa_grd"));
                    obj.setCode(jsonObject.getString("code"));
                    obj.setPkWaItem(jsonObject.getString("pk_wa_item"));
                    obj.setPkOrg(jsonObject.getString("pk_org"));
                    viewService.saveWaGrad(obj);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(resultJson.getString("message"));
        }

            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_wa_grade_v");
            ncclog.setContent(requstBody);
            ncclog.setName("薪资标准类别视图成功清空并添加" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("薪资标准类别视图成功清空并添加" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("薪资标准类别视图失败添加|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_wa_grade_v");
            ncclog.setContent("");
            ncclog.setName("薪资标准类别视图失败添加");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }

    /*
     * 公共薪资项目
     * */
    @RequestMapping("hr_wa_item_v")
    @ResponseBody
    public BaseWrapper waitem(String view) throws Exception {

        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            String requstBody = "{\"view\":\"hr_wa_item_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);

            int  add=0;
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson.getIntValue("code") == 1){
                if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                viewService.truncateTable("ncc_wa_item_v");
                Map<String, Object> psnMap = new HashMap<>();
                JSONArray dataArray = resultJson.getJSONArray("data");
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);
                    NccWaItemV obj = new NccWaItemV();
                    obj.setName(jsonObject.getString("name"));
                    obj.setCode(jsonObject.getString("code"));
                    obj.setPkWaItem(jsonObject.getString("pk_wa_item"));
                    obj.setPkOrg(jsonObject.getString("pk_org"));
                    viewService.saveWaItem(obj);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(resultJson.getString("message"));
        }

            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_wa_item_v");
            ncclog.setContent(requstBody);
            ncclog.setName("公共薪资项目视图成功清空并添加"+add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("公共薪资项目视图成功清空并添加"+add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);
        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("公共薪资项目视图失败添加|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_wa_item_v");
            ncclog.setContent("");
            ncclog.setName("公共薪资项目视图失败添加");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }

    /*
     * 异动类型
     * */
    @RequestMapping("hr_trnstype_v")
    @ResponseBody
    public BaseWrapper trnstype(String view) throws Exception {

        BaseWrapper baseWrapper=new BaseWrapper();
        try{
            //请求获取数据
            String requstBody = "{\"view\":\"hr_trnstype_v\"}";
            String result = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);

            int  add=0;
            //将数据封装JSON
            JSONObject resultJson = JSONObject.parseObject(result);

            //是否成功
            if (resultJson.getIntValue("code") == 1){
        if(null!=resultJson.getJSONArray("data").toJSONString()&&!resultJson.getJSONArray("data").toJSONString().equals("[]")){
                //清空表
                viewService.truncateTable("ncc_trnstype_v");

                //封装data数据为json数组
                JSONArray dataArray = resultJson.getJSONArray("data");

                //遍历数组添加数据库
                for (int i=0; i<dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);

                    NccTrnstypeV obj = new NccTrnstypeV();
                    obj.setPkTrnstype(jsonObject.getString("pk_trnstype"));
                    obj.setTrnsevent(jsonObject.getString("trnsevent"));
                    obj.setTrnstypecode(jsonObject.getString("trnstypecode"));
                    obj.setTrnstypename(jsonObject.getString("trnstypename"));
                    viewService.saveTrnstype(obj);
                    add ++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(resultJson.getString("message"));
        }
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_trnstype_v");
            ncclog.setContent(requstBody);
            ncclog.setName("异动类型视图成功清空并添加" + add);
            ncclog.setResultLong(result);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            baseWrapper.setFlag(true);
            baseWrapper.setMsg("异动类型视图成功清空并添加" + add);
            baseWrapper.setStatus(true);
            baseWrapper.setData(result);

        }catch (Exception e){
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("异动类型视图失败添加|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            // 接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_trnstype_v");
            ncclog.setContent("");
            ncclog.setName("异动类型视图失败添加");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;
    }
    /*
     * 岗位插入
     * */
    @RequestMapping("hr_post_v")
    @ResponseBody
    public BaseWrapper hrpostv(String view) throws Exception {
        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            //=====1、请求地址获取岗位
            String requstBody = "{\"view\":\"hr_post_v\"}";

            String orgResult = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONArray  orgResultJsonArray= JSON.parseObject(orgResult).getJSONArray("data");

            int add=0,modify=0,del=0;
            if (JSON.parseObject(orgResult).getIntValue("code") == 1) {
                if(null!=orgResultJsonArray.toJSONString()&&!orgResultJsonArray.toJSONString().equals("[]")){

                    //清空表
                    viewService.truncateTable("ncc_post_v");
                    /*List<NccPostV> psninfoList = nccPostVService.getNccPostVListByMap(null);
                Map<String, Object> psnMap = new HashMap<>();
                if (null != psninfoList && psninfoList.size() != 0) {
                    for (NccPostV psninfo : psninfoList) {
                        psnMap.put(psninfo.getPostcode(), psninfo);
                    }
                }*/


                //=====2、遍历组织json数组做相应操作
                for (int i = 0; i < orgResultJsonArray.size(); i++) {
                    NccPostV nccPostV = new NccPostV();
                    nccPostV.setDeptname(orgResultJsonArray.getJSONObject(i).getString("deptname"));
                    nccPostV.setOrgname(orgResultJsonArray.getJSONObject(i).getString("orgname"));
                    nccPostV.setPkDept(orgResultJsonArray.getJSONObject(i).getString("pk_dept"));
                    nccPostV.setPkOrg(orgResultJsonArray.getJSONObject(i).getString("pk_org"));
                    nccPostV.setPkPost(orgResultJsonArray.getJSONObject(i).getString("pk_post"));
                    nccPostV.setPostcode(orgResultJsonArray.getJSONObject(i).getString("postcode"));
                    nccPostV.setPostname(orgResultJsonArray.getJSONObject(i).getString("postname"));
                    add++;
                    nccPostVService.itriptxAddNccPostV(nccPostV);

                    /*if (!psnMap.containsKey(nccPostV.getPostcode())){
                        // 不存在的 add
                        add++;
                        nccPostVService.itriptxAddNccPostV(nccPostV);
                    }else{
                        // 存在的 修改
                        modify++;
                        nccPostVService.itriptxModifyNccPostV(nccPostV);
                        //在同步表删除已修改过的
                        psnMap.remove(nccPostV.getPostcode());
                    }*/
                }
                //剩下未修改的进行删除
                /*if(psnMap.size()!=0&&null!=psnMap){

                    Set<Map.Entry<String, Object>> entries = psnMap.entrySet();
                    Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                    while (iterator.hasNext()){
                        nccPostVService.itriptxDeleteNccPostVById(((NccPostV) iterator.next().getValue()).getId());
                        del++;
                    }
                }*/
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(JSON.parseObject(orgResult).getString("message"));
        }
            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_post_v");
            ncclog.setContent(requstBody);
            ncclog.setName("岗位视图同步成功"+"添加"+add+"修改"+modify+"删除"+del);
            ncclog.setResultLong(orgResult);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

            //=====3、成功
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("岗位视图同步成功新增岗位：" + add + ";修改：" + modify);
            baseWrapper.setStatus(true);

        }catch (Exception e){
            //=====、失败
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("岗位视图同步失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);
            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_post_v");
            ncclog.setContent("");
            ncclog.setName("岗位视图同步错误");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;

    }
    /*
     * 人员类别
     * */
    @RequestMapping("hr_psncl_v")
    @ResponseBody
    public BaseWrapper hrpsnclv(String view) throws Exception {
        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            //=====1、请求地址获取岗位
            String requstBody = "{\"view\":\"hr_psncl_v\"}";
            String orgResult = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONArray  orgResultJsonArray= JSON.parseObject(orgResult).getJSONArray("data");
            int add=0;

            //是否成功
            if (JSON.parseObject(orgResult).getIntValue("code") == 1) {
if(null!=orgResultJsonArray.toJSONString()&&!orgResultJsonArray.toJSONString().equals("[]")){
                //清空表
                viewService.truncateTable("ncc_psncl_v");
                //=====2、遍历组织json数组做相应操作
                for (int i = 0; i < orgResultJsonArray.size(); i++) {

                    NccPsnclV nccPsnclV = JSON.toJavaObject(orgResultJsonArray.getJSONObject(i), NccPsnclV.class);
                    nccPsnclV.setPostcode(orgResultJsonArray.getJSONObject(i).get("code").toString());
                    nccPsnclV.setPostname(orgResultJsonArray.getJSONObject(i).get("name").toString());
                    nccPsnclVService.itriptxAddNccPsnclV(nccPsnclV);
                    add++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(JSON.parseObject(orgResult).getString("message"));
        }
            //=====3、成功
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("人员类别视图同步成功清空并添加"+add);
            baseWrapper.setStatus(true);
            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_post_v");
            ncclog.setContent(requstBody);
            ncclog.setName("人员类别视图同步成功清空并添加"+add);
            ncclog.setResultLong(orgResult);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

        }catch (Exception e){
            //=====、失败
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("人员类别视图同步失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);

            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_psncl_v");
            ncclog.setContent("");
            ncclog.setName("人员类别视图同步失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;

    }


    /*
     * 新岗位序列
     * */
    @RequestMapping("hr_postseries_v")
    @ResponseBody
    public BaseWrapper hrpostseriesv(String view) throws Exception {
        BaseWrapper baseWrapper=new BaseWrapper();
        try {
            //=====1、请求地址获取岗位
            String requstBody = "{\"view\":\"hr_postseries_v\"}";
            String orgResult = nccloudService.sendApi(nccloudService.queryViewUrl,requstBody);
            JSONArray  orgResultJsonArray= JSON.parseObject(orgResult).getJSONArray("data");

            int add=0;
            //是否成功
            if (JSON.parseObject(orgResult).getIntValue("code") == 1) {
                if(null!=orgResultJsonArray.toJSONString()&&!orgResultJsonArray.toJSONString().equals("[]")){
                //清空表
                viewService.truncateTable("ncc_postseries_v");
                //=====2、遍历组织json数组做相应操作
                for (int i = 0; i < orgResultJsonArray.size(); i++) {

                    NccPostseriesV nccPostseriesV=new NccPostseriesV();
                    nccPostseriesV.setPkGroup(orgResultJsonArray.getJSONObject(i).getString("pk_group"));
                    nccPostseriesV.setPkOrg(orgResultJsonArray.getJSONObject(i).getString("pk_org"));
                    nccPostseriesV.setPkPostseries(orgResultJsonArray.getJSONObject(i).getString("pk_postseries"));
                    nccPostseriesV.setPostseriescode(orgResultJsonArray.getJSONObject(i).getString("postseriescode"));
                    nccPostseriesV.setPostseriesname(orgResultJsonArray.getJSONObject(i).getString("postseriesname"));
                    nccPostseriesVService.itriptxAddNccPostseriesV(nccPostseriesV);
                    add++;
                }
            }else {
                throw new RuntimeException("请求NC视图data-null");
            }
        }else {
        throw  new RuntimeException(JSON.parseObject(orgResult).getString("message"));
        }
            //=====3、成功
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("新岗位序列视图同步成功清空并添加"+add);
            baseWrapper.setStatus(true);
            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_postseries_v");
            ncclog.setContent(requstBody);
            ncclog.setName("新岗位序列视图同步成功清空并添加"+add);
            ncclog.setResultLong(orgResult);
            ncclog.setState("1");
            ncclog.setDisposeState("0");
            logService.save(ncclog);

        }catch (Exception e){
            //=====、失败
            baseWrapper.setFlag(false);
            baseWrapper.setMsg("新岗位序列视图同步失败|message:"+e.getMessage());
            baseWrapper.setStatus(true);

            //接口调用日志保存
            NCCInterfaceLog ncclog = new NCCInterfaceLog();
            ncclog.setBusinessNo("hr_postseries_v");
            ncclog.setContent("");
            ncclog.setName("新岗位序列视图同步失败");
            ncclog.setResultLong(e.getMessage());
            ncclog.setState("0");
            ncclog.setDisposeState("0");
            logService.save(ncclog);
        }
        return baseWrapper;

    }






}